package br.com.napoleao.controlfin.util;

import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class GUIUtil
{
	public static void centralizeShell(Shell inner, Shell outer)
	{
		inner.setLocation( outer.getLocation().x + (outer.getSize().x/2) - (inner.getSize().x/2),
				           outer.getLocation().y + (outer.getSize().y/2) - (inner.getSize().y/2));
	}

	public static void centralizeShell(Shell shell)
	{
		Rectangle screenBounds = shell.getDisplay().getBounds();
		Rectangle shellBounds = shell.getBounds();
		
		int wScreenCenter = screenBounds.width  / 2;
		int hScreenCenter = screenBounds.height / 2;
		
		int wShellCenter = shellBounds.width  / 2;
		int hShellCenter = shellBounds.height / 2;
        
          // Set shell bounds,
		shell.setBounds(wScreenCenter - wShellCenter, hScreenCenter - hShellCenter, shellBounds.width, shellBounds.height);
	}
	
	public static Shell createCentralizedShell(String title, Integer width, Integer height)
	{
		Display display = Display.getCurrent(); if(display == null) display = new Display();
		Shell shell = new Shell(display);
		
		shell.setLayout(new FillLayout());
		shell.setText(title);
		shell.setSize(width, height);
		GUIUtil.centralizeShell(shell);
		
		return shell;
	}
}